/*
-----------------------------------------------------------------------------
Filename:    Projectile.cpp
Author: Michael Sebright
-----------------------------------------------------------------------------
Provides functions for a projectile object
*/

#include "Projectile.h"
#include "conio.h"


Projectile::Projectile() {
	velocity = 0;
	isActive = false;
}

Projectile::Projectile(SceneManager* sceneMgr) {
	velocity = 0;
	isActive = false;
	setUpProjectile(sceneMgr);	
}

void Projectile::setUpProjectile(SceneManager* sceneMgr) {
	mProjectileNode = sceneMgr->getRootSceneNode()->createChildSceneNode();
	mProjectileObj = sceneMgr->createManualObject();

	mProjectileObj->begin("BaseWhiteNoLighting", RenderOperation::OT_LINE_LIST);

	mProjectileObj->position(0, 5, 0);
	mProjectileObj->position(0, 5, -10);
 
	mProjectileObj->end();

	mProjectileObj->setVisible(false);
 
	
	mProjectileNode->attachObject(mProjectileObj);
	
}

void Projectile::fire(Vector3 initialPosition, Quaternion direction) {
	Projectile::direction = direction;
	_cprintf("in projFire\n");
	mProjectileNode->setPosition(initialPosition);
	mProjectileNode->setOrientation(direction);
	srand(time(NULL));
	accuracy = rand() % 20;
	if(rand() % 2) {
		accuracy *= -1;
	}
	mProjectileNode->rotate(Vector3::UNIT_Y, accuracy);
	mProjectileObj->setVisible(true);
	isActive = true;
}

void Projectile::update(Real deltaTime, Camera* cam) {
	mProjectileNode->translate(velocity * deltaTime * direction.zAxis().x, 0, velocity * deltaTime * direction.zAxis().z);
	if(!cam->isVisible(mProjectileNode->_getWorldAABB())) {
		isActive = false;
		mProjectileObj->setVisible(false);
	}
}


bool Projectile::isProjectileActive() {
	return isActive;
}